package com.sscaipiao.android.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.widget.Toast;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.Lottery;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.model.TogetherBuy;


public class CaipiaoServiceHelper {


	public static void getBetHmHall(Context context, List<TogetherBuy> list,User user, String lotteryCode, String betMoney,
	                                             String currShare, String schedule, String baoDi, String status, String nPageIndex, String nPageSize) {
		String uri = Constants.SERVER_URL + "getBetHmHall.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		if (StringUtil.isNotEmptyString(lotteryCode)) {
			pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));
		}
		if (StringUtil.isNotEmptyString(betMoney)) {
			pairs.add(new BasicNameValuePair("betMoney", betMoney));
		}
		if (StringUtil.isNotEmptyString(currShare)) {
			pairs.add(new BasicNameValuePair("currShare", currShare));
		}
		if (StringUtil.isNotEmptyString(nPageIndex)) {
			pairs.add(new BasicNameValuePair("nPageIndex", nPageIndex));
		}
		if (StringUtil.isNotEmptyString(nPageSize)) {
			pairs.add(new BasicNameValuePair("nPageSize", nPageSize));
		}
		if (StringUtil.isNotEmptyString(schedule)) {
			pairs.add(new BasicNameValuePair("schedule", schedule));
		}
		if (StringUtil.isNotEmptyString(baoDi)) {
			pairs.add(new BasicNameValuePair("baoDi", baoDi));
		}
		if (StringUtil.isNotEmptyString(status)) {
			pairs.add(new BasicNameValuePair("status", status));
		}

		NetworkManager nm = new NetworkManager(context);
		String retStr = "";
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);
			JSONArray jsonArray = jsonObject.getJSONArray(Constants.RECORD_LIST);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				TogetherBuy tb = new TogetherBuy();
				if (object.has("betMoney")) {
					tb.setBetMoney(object.getString("betMoney"));
				}
				if (object.has("fee")) {
					tb.setFee(object.getString("fee"));
				}
				if (object.has("lotteryCode")) {
					tb.setLotteryCode(object.getString("lotteryCode"));
				}
				if (object.has("lotteryName")) {
					tb.setLotteryName(object.getString("lotteryName"));
				}
				if (object.has("orderNo")) {
					tb.setOrderNo(object.getString("orderNo"));
				}
				if (object.has("periodNo")) {
					tb.setPeriodNo(object.getString("periodNo"));
				}
				if (object.has("price")) {
					tb.setPrice(object.getString("price"));
				}
				if (object.has("promoter")) {
					tb.setPromoter(object.getString("promoter"));
				}
				if (object.has("schedule")) {
					tb.setSchedule(object.getString("schedule"));
				}
				if (object.has("strState")) {
					tb.setStrState(object.getString("strState"));
				}
				if (object.has("surplus")) {
					tb.setSurplus(object.getString("surplus"));
				}
				if (object.has("title")) {
					tb.setTitle(object.getString("title"));
				}
				list.add(tb);
			}

			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	public static List<String> getBetList(Context context, User user, String lotteryCode, String startDate,
	                                      String endDate, String nPageIndex, String nPageSize, String isAward) {
		String uri = Constants.SERVER_URL + "getBetList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		if (StringUtil.isNotEmptyString(lotteryCode)) {
			pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));
		}
		if (StringUtil.isNotEmptyString(startDate)) {
			pairs.add(new BasicNameValuePair("startDate", startDate));
		}
		if (StringUtil.isNotEmptyString(endDate)) {
			pairs.add(new BasicNameValuePair("endDate", endDate));
		}
		if (StringUtil.isNotEmptyString(nPageIndex)) {
			pairs.add(new BasicNameValuePair("nPageIndex", nPageIndex));
		}
		if (StringUtil.isNotEmptyString(nPageSize)) {
			pairs.add(new BasicNameValuePair("nPageSize", nPageSize));
		}
		if (StringUtil.isNotEmptyString(isAward)) {
			pairs.add(new BasicNameValuePair("isAward", isAward));
		}

		List<String> list = new ArrayList<String>();
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);

			String  retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.MESSAGE);
			if (!retStr.equals("01")) {
				//Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			JSONArray jsonArray = jsonObject.getJSONArray(Constants.RECORD_LIST);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);

				list.add(object.getString("betTime") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("lotteryCode") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("lotteryName") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("periodNo") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("lotteryNum") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("betMoney") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("awardInfo") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("awardMoney"));
			}

			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}

	public static String payLotteryDoubleColorBall(Context context, User user, String periodNo, String times, String total) {
		String uri = Constants.SERVER_URL + "betSSQ.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		pairs.add(new BasicNameValuePair("periodNo", periodNo));

		String schemeNum1 = combineSchemeNum1(user, times, total);
		pairs.add(new BasicNameValuePair("schemeNum1", schemeNum1));

		Log.v("CaipiaoServiceHelper.schemeNum1", schemeNum1);

		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.responseStr", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);

			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.MESSAGE);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return retStr;
			}
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return retStr;
	}

	public static String payLotteryRedballSingleRedball(Context context, User user, String periodNo, String times, String total, String doFunction) {
		String uri = Constants.SERVER_URL + doFunction;//"betQLC.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		pairs.add(new BasicNameValuePair("periodNo", periodNo));

		String schemeNum1 = combineSchemeNum1(user, times, total);
		pairs.add(new BasicNameValuePair("schemeNum1", schemeNum1));

		Log.v("CaipiaoServiceHelper.schemeNum1", schemeNum1);

		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.responseStr", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);

			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.MESSAGE);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return retStr;
			}
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return retStr;
	}

	public static String betZHSSQ(Context context, User user, String periodNo, String times, String total, String isBonusBreak, String reward) {
		String uri = Constants.SERVER_URL + "betZHSSQ.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		pairs.add(new BasicNameValuePair("pno", periodNo));
		pairs.add(new BasicNameValuePair("bs", times));
		pairs.add(new BasicNameValuePair("allPay", total));
		if (StringUtil.isNotEmptyString(isBonusBreak)) {
			pairs.add(new BasicNameValuePair("isBonusBreak", isBonusBreak));
		}
		if (StringUtil.isNotEmptyString(reward)) {
			pairs.add(new BasicNameValuePair("reward", reward));
		}


		String schemeNum1 = combineSchemeNum1(user, times, total);
		pairs.add(new BasicNameValuePair("schemeNum1", schemeNum1));

		Log.v("CaipiaoServiceHelper.schemeNum1", schemeNum1);

		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.responseStr", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);

			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.MESSAGE);


			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return retStr;
	}

	public static String betZhuiHaoRedballSingleArray(Context context, User user, String periodNo, String times, String total, String isBonusBreak, String reward, String doFunction) {
		String uri = Constants.SERVER_URL + doFunction;
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		pairs.add(new BasicNameValuePair("pno", periodNo));
		pairs.add(new BasicNameValuePair("bs", times));
		pairs.add(new BasicNameValuePair("allPay", total));
		if (StringUtil.isNotEmptyString(isBonusBreak)) {
			pairs.add(new BasicNameValuePair("isBonusBreak", isBonusBreak));
		}
		if (StringUtil.isNotEmptyString(reward)) {
			pairs.add(new BasicNameValuePair("reward", reward));
		}


		String schemeNum1 = combineSchemeNum1(user, times, total);
		pairs.add(new BasicNameValuePair("schemeNum1", schemeNum1));

		Log.v("CaipiaoServiceHelper.schemeNum1", schemeNum1);

		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.responseStr", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);

			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.MESSAGE);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return retStr;
			}
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return retStr;
	}

	public static String betHMSSQ(Context context, User user, String schemeNum1, String periodNo,
	                              String title, String fee, String allshare, String currshare, String friends, String baodi) {
		String uri = Constants.SERVER_URL + "betHMSSQ.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		pairs.add(new BasicNameValuePair("schemeNum1", schemeNum1));
		pairs.add(new BasicNameValuePair("periodNo", periodNo));
		pairs.add(new BasicNameValuePair("title", title));
		pairs.add(new BasicNameValuePair("fee", fee));
		pairs.add(new BasicNameValuePair("allshare", allshare));
		pairs.add(new BasicNameValuePair("currshare", currshare));
		if (StringUtil.isNotEmptyString(friends)) {
			pairs.add(new BasicNameValuePair("friends", friends));
		}
		pairs.add(new BasicNameValuePair("baodi", baodi));


		Log.v("CaipiaoServiceHelper.schemeNum1", schemeNum1);

		String retStr = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.responseStr", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);

			retStr = jsonObject.getString(Constants.STATUS) + Constants.STRING_SPLITE_COMPLEX +
					jsonObject.getString(Constants.MESSAGE);


			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return retStr;
	}

	/**
	 * @param user
	 * @param times
	 * @param total
	 * @return
	 */
	public static String combineSchemeNum1(User user, String times,
	                                       String total) {
		String schemeNum1 = "{\"";
		for (String choosen : user.getChoosen()) {
			String[] split = choosen.split(Constants.STRING_SPLITE_COMPLEX);

			schemeNum1 += split[0];
			schemeNum1 += "|";
			schemeNum1 += split[1];
			schemeNum1 += "#1_";
			schemeNum1 += split[2].equals("单式投注") ? "1_" : "2_";
			schemeNum1 += (times + "_");
			schemeNum1 += (total + "\",");
		}
		schemeNum1 = schemeNum1.substring(0, schemeNum1.length() - 1);
		schemeNum1 += "}";
		return schemeNum1;
	}

	//	periodNo:期数
	//	dlineTime:开奖时间
	//	part1: 中奖号
	//	part2: 中奖号
	//	status: 01 获取信息成功
	//	02 必填字段为空
	//	03 获取失败
	//	100 其他异常

	public static List<String> getLottHistoryList(Context context, User user, String lotteryCode) {
		String uri = Constants.SERVER_URL + "getLottHistoryList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));

		List<String> list = new ArrayList<String>();
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			JSONArray jsonArray = jsonObject.getJSONArray(Constants.PERIOD_LIST);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				list.add(object.getString("periodNo") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("part1") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("part2").replaceAll(",", " ") + Constants.STRING_SPLITE_COMPLEX +
						object.getString("DlineTime"));
			}

			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}

	public static List<String> getLottLostValues_DoubleBall(Context context, User user, String lotteryCode) {
		String uri = Constants.SERVER_URL + "getOmisCodeList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));

		List<String> list = new ArrayList<String>();
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			JSONArray jsonArray = jsonObject.getJSONArray("part1");

			for (int i = 0; i < jsonArray.length(); i++) {
				Object object = jsonArray.get(i);
				list.add(object.toString());
			}

			jsonArray = jsonObject.getJSONArray("part2");
			for (int i = 0; i < jsonArray.length(); i++) {
				Object object = jsonArray.get(i);
				list.add(object.toString());
			}
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}

	public static List<String> getLottLostValues_SingleRedBall(Context context, User user, String lotteryCode) {
		String uri = Constants.SERVER_URL + "getOmisCodeList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));

		List<String> list = new ArrayList<String>();
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			JSONArray jsonArray = jsonObject.getJSONArray("part1");

			for (int i = 0; i < jsonArray.length(); i++) {
				Object object = jsonArray.get(i);
				list.add(object.toString());
			}
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}
	public static List<String> getLottLostValues_NPart(Context context, User user, String lotteryCode,int parts) {
		String uri = Constants.SERVER_URL + "getOmisCodeList.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));

		List<String> list = new ArrayList<String>();
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			if (parts >= 1) {
				JSONArray jsonArray = jsonObject.getJSONArray("part1");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			if (parts >= 2) {
				JSONArray jsonArray = jsonObject.getJSONArray("part2");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			if (parts >= 3) {
				JSONArray jsonArray = jsonObject.getJSONArray("part3");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			if (parts >= 4) {
				JSONArray jsonArray = jsonObject.getJSONArray("part4");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			if (parts >= 5) {
				JSONArray jsonArray = jsonObject.getJSONArray("part5");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			if (parts >= 6) {
				JSONArray jsonArray = jsonObject.getJSONArray("part6");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			if (parts >= 7) {
				JSONArray jsonArray = jsonObject.getJSONArray("part7");
				for (int i = 0; i < jsonArray.length(); i++) {
					Object object = jsonArray.get(i);
					list.add(object.toString());
				}
			}
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}
	/**
	 * @param context
	 * @param user
	 * @return "type,code,name"
	 */
	public static List<String> getAllLottery(Context context, User user) {
		String uri = Constants.SERVER_URL + "getAllLottery.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		List<String> list = new ArrayList<String>();
		String dupliate = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.getAllLottery", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			JSONArray jsonArray = jsonObject.getJSONArray(Constants.LOTTERY_LIST);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				if (dupliate.indexOf("," + object.getString("type") + ",") > 0) {
					continue;
				}
				if (!object.has("name")) {
					continue;
				}
				list.add(object.getString("type") + Constants.STRING_SPLITE +
						object.getString("code") + Constants.STRING_SPLITE +
						object.getString("name"));

				dupliate += ("," + object.getString("type") + ",");
			}

			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}
//	periodInfo={"lotteryType":107,
// "moneyList":[{"money":0,"count":0,"name":"一等奖"},{"money":0,"count":0,"name":"二等奖"},{"money":0,"count":0,"name":"三等奖"},{"money":0,"count":0,"name":"四等奖"},{"money":0,"count":0,"name":"五等奖"},{"money":0,"count":0,"name":"六等奖"}],
// "periodNo":"2014007","currPool":0,"betCount":0,"dLineTime":"2014-01-15 19:57:00.0"}
	public static String  getLottery(Context context, User user,String lotteryCode) {
		String info = "";
		String uri = Constants.SERVER_URL + "getLottery.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));
		pairs.add(new BasicNameValuePair("lotteryCode", lotteryCode));
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return "";
			}
			JSONObject jsonLottInfo = new JSONObject(jsonObject.getString("periodInfo"));
			info = jsonLottInfo.getString("periodNo") + Constants.STRING_SPLITE_COMPLEX +
					jsonLottInfo.getString("currPool") + Constants.STRING_SPLITE_COMPLEX +
					jsonLottInfo.getString("betCount") + Constants.STRING_SPLITE_COMPLEX +
					jsonLottInfo.getString("dLineTime") + Constants.STRING_SPLITE_COMPLEX;
			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return info;
	}
	public static List<String> getHotLottery(Context context, User user) {
		String uri = Constants.SERVER_URL + "getHotLottery.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		List<String> list = new ArrayList<String>();
		String dupliate = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);
			Log.v("CaipiaoServiceHelper.getHotLottery", responseStr);
			JSONObject jsonObject = new JSONObject(responseStr);

			JSONArray jsonArray = jsonObject.getJSONArray(Constants.LOTTERY_LIST);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				if (dupliate.indexOf("," + object.getString("type") + ",") > 0) {
					continue;
				}
				list.add(object.getString("type"));
				dupliate += ("," + object.getString("type") + ",");
			}

			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}


	public static List<Lottery> getAllLotteryAward(Context context, User user) {
		String uri = Constants.SERVER_URL + "getAllLotteryAward.do";
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		pairs.add(new BasicNameValuePair("token", user.getToken()));

		List<Lottery> list = new ArrayList<Lottery>();
		String dupliate = "";
		NetworkManager nm = new NetworkManager(context);
		try {
			String responseStr = nm.SendAndWaitResponse(pairs, uri);

			JSONObject jsonObject = new JSONObject(responseStr);
			String retStr = jsonObject.getString(Constants.STATUS);
			if (!retStr.equals("01")) {
				Toast.makeText(context, jsonObject.getString(Constants.MESSAGE), Toast.LENGTH_SHORT).show();
				return list;
			}
			JSONArray jsonArray = jsonObject.getJSONArray(Constants.PERIOD_LIST);

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);

				if (dupliate.indexOf("," + object.getString("lotteryType") + ",") > 0) {
					continue;
				}

				Lottery lottery = new Lottery();
				lottery.setType(object.getString("lotteryType"));
				lottery.setPeriodNo(object.getLong("periodNo"));
				lottery.setBetCount(object.getLong("betCount"));
				lottery.setCurrPool(object.getLong("currPool"));
				lottery.setdLineTime(DateUtil.getDate(object.getString("dLineTime")));
				list.add(lottery);
				dupliate += ("," + object.getString("lotteryType") + ",");
			}

			user.setToken(jsonObject.getString(Constants.TOKEN));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}

}
